package com.ms.education.ucenter.controller;

import cn.dev33.satoken.annotation.SaCheckLogin;
import cn.dev33.satoken.stp.StpUtil;
import cn.hutool.core.convert.Convert;
import com.alibaba.nacos.common.util.Md5Utils;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.ms.education.JwtUtils;
import com.ms.education.MD5Util;
import com.ms.education.R;
import com.ms.education.exceptionHandler.EduException;
import com.ms.education.ucenter.entity.Admin;
import com.ms.education.ucenter.entity.User;
import com.ms.education.ucenter.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.Map;

/**
 * <p>Project:education-AdminController
 * <p>Powered by ms on 2023-01-12 22:32:04
 *
 * @author ms
 * @version 1.0
 * @since 1.8
 */
@RestController
@RequestMapping("/ucenter/admin")
public class AdminController {

    @Autowired
    private UserService userService;


    @PostMapping("login")
    public R login(@RequestBody Admin admin) {
        String mobile = admin.getUsername();
        String password = admin.getPassword();
        QueryWrapper<User> wrapper = new QueryWrapper<User>().eq("mobile", mobile).eq("password", MD5Util.getMD5(password));
        User user = userService.getOne(wrapper);
        if (user == null) {
            throw new EduException(20001, "请求错误");
        }
        StpUtil.login(user.getId());
        String token = StpUtil.getTokenInfo().getTokenValue();
        return R.ok().data("token", token);
    }

    @SaCheckLogin
    @GetMapping("info")
    public R info(String token) {
        Map<String, Object> res = userService.getUserInfo(Convert.toInt(StpUtil.getLoginId()));
        StpUtil.getRoleList();
        StpUtil.getPermissionList();
        return R.ok().data(res);

    }

    @SaCheckLogin
    @PostMapping("logout")
    public R logout() {
        StpUtil.logout();
        return R.ok();
    }
}
